import json

from flask import jsonify, request, session, make_response, Response

from models import db
from models.index import User, Collection, Comment, CommentLike
from . import news_blu


@news_blu.route("/news/collect", methods=["POST"])
def news_collect():
    # 1. 提取参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")  # do表示关注 undo表示取消关注

    # 2. 获取当前用户的id
    user_id = session.get("user_id")

    # 3. 如果是关注功能
    if action == "do":
        collection = Collection()
        collection.news_id = news_id
        collection.user_id = user_id
        try:
            db.session.add(collection)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "成功"
            }
        except Exception as ret:
            ret = {
                "errno": 5001,
                "errmsg": "关注失败..."
            }

    elif action == "undo":
        collection = db.session.query(Collection).filter(Collection.user_id == user_id,
                                                         Collection.news_id == news_id).first()
        if collection:
            db.session.delete(collection)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "取消收藏成功"
            }
        else:
            ret = {
                "errno": 5002,
                "errmsg": "取消收藏失败"
            }

    return jsonify(ret)


@news_blu.route("/news/comment", methods=["POST"])
def news_comment():
    # 1. 提取出用户评价时的数据
    content = request.json.get("content")
    news_id = request.json.get("news_id")
    parent_id = request.json.get("parent_id")
    user_id = session.get("user_id")

    # 2. 保存到数据库
    new_comment = Comment()
    new_comment.news_id = news_id
    new_comment.user_id = user_id
    new_comment.content = content
    new_comment.parent_id = parent_id
    db.session.add(new_comment)
    db.session.commit()
    # comment = {
    #     'user': {'avatar_url': new_comment.user.avatar_url, 'nick_name': new_comment.user.nick_name},
    #     'parent': {'content': new_comment.parent.content, 'user': {'nick_name': new_comment.parent.user.nick_name}},
    #     'content': new_comment.content,
    #     'create_time': new_comment.create_time,
    #     'id': new_comment.id,
    #     'news_id': new_comment.news_id
    #
    # }
    if parent_id:
        user = new_comment.user
        parent = new_comment.parent
        p_user = new_comment.parent.user

        new_comment = new_comment.__dict__
        user = user.__dict__
        parent = parent.__dict__
        p_user = p_user.__dict__

        del new_comment['_sa_instance_state']
        del user['_sa_instance_state']
        del parent['_sa_instance_state']
        # del p_user['_sa_instance_state']

        parent['user'] = p_user
        new_comment['user'] = user
        new_comment['parent'] = parent
    else:
        new_comment = '评论'

    ret = {

        "errno": 0,
        "errmsg": "成功",
        'comment': new_comment

    }

    return jsonify(ret)


@news_blu.route('/news/comment_like', methods=['POST'])
def news_comment_like():
    action = request.json.get('action')
    user_id = session.get('user_id')
    comment_id = request.json.get('comment_id')
    if action == 'do':

        comment_like = CommentLike()
        comment_like.user_id = user_id
        comment_like.comment_id = comment_id
        db.session.add(comment_like)
        db.session.commit()

        ret = {
            'errno': 0,
            'errmsg': '点赞成功'

        }

    elif action == 'undo':
        comment_like = db.session.query(CommentLike).filter(CommentLike.user_id == user_id,
                                                            CommentLike.comment_id == comment_id).first()
        if comment_like:

            db.session.delete(comment_like)
            db.session.commit()
            ret = {
                'errno': 0,
                'errmsg': '取消点赞成功'

            }

        else:
            ret = {
                'errno': 6002,
                'errmsg': '取消点赞失败'

            }

    else:
        ret = {
            'errno': 6001,
            'errmsg': '非法请求'

        }

    return jsonify(ret)
